﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Resources;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace DataFileApplication_WPF.Helper
{
    public class LanguageManager
    {
        private static ResourceManager _resourceManager;
        private static CultureInfo _currentCulture;

        static LanguageManager()
        {
            // 初始化资源管理器，这里假设资源文件名为 Strings，位于项目的 Resources 命名空间下
            _resourceManager = new ResourceManager("DataFileApplication_WPF.Resources.Language.Strings", typeof(LanguageManager).Assembly);
            _currentCulture = CultureInfo.CurrentCulture;
        }

        public static CultureInfo CurrentCulture
        {
            get => _currentCulture;
            set
            {
                if (_currentCulture != value)
                {
                    _currentCulture = value;
                    UpdateResources();
                }
            }
        }

        private static void UpdateResources()
        {
            CultureInfo.CurrentCulture = _currentCulture;
            CultureInfo.CurrentUICulture = _currentCulture;

            var resources = _resourceManager.GetResourceSet(_currentCulture, true, true);
            if (resources != null)
            {
                foreach (System.Collections.DictionaryEntry entry in resources)
                {
                    Application.Current.Resources[entry.Key.ToString()] = entry.Value;
                }
            }
        }

        public static string GetString(string key)
        {
            return _resourceManager.GetString(key, _currentCulture);
        }

       public static string GetCurrentLanguage()
        {
            CultureInfo currentCulture = Thread.CurrentThread.CurrentUICulture;
            return currentCulture != null ? currentCulture.Name : null;
        }
    }
}
